package com.smartfactory.sail.controller;


import com.ada.base.common.ApiResponse;
import com.ada.base.common.ControllerResponse;
import com.ada.base.common.ResultData;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.smartfactory.sail.config.HeaderWrapper;
import com.smartfactory.sail.config.OperRecord;
import com.smartfactory.sail.constant.header.DeviceEnum;
import com.smartfactory.sail.entity.Device;
import com.smartfactory.sail.entity.DeviceMaintenance;
import com.smartfactory.sail.entity.enums.DeviceStatusEnum;
import com.smartfactory.sail.service.IDeviceMaintenanceService;
import com.smartfactory.sail.service.IDeviceService;
import com.smartfactory.sail.vo.PageParam;
import com.smartfactory.sail.vo.device.AddDeviceReq;
import com.smartfactory.sail.vo.device.ListDeviceResp;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 设备信息 前端控制器
 * </p>
 *
 * @author 王巨壁
 * @since 2019-07-24
 */
@Api(tags ="设备管理")
@RestController
@RequestMapping("/device")
public class DeviceController extends ControllerResponse {

    private IDeviceService iDeviceService;
    private IDeviceMaintenanceService iDeviceMaintenanceService;

    public DeviceController(IDeviceService iDeviceService, IDeviceMaintenanceService iDeviceMaintenanceService) {
        this.iDeviceService = iDeviceService;
        this.iDeviceMaintenanceService = iDeviceMaintenanceService;
    }

    @OperRecord(content = "新增设备",level = 1)
    @PostMapping("add")
    @ApiOperation(value = "设备新增",notes = "设备新增")
    public ApiResponse<Long> add(@RequestBody AddDeviceReq addDeviceReq) {
        Device device = new Device();

        BeanUtils.copyProperties(addDeviceReq,device);
        if (iDeviceService.save(device))
            return ok(device.getId());
        return fail(ResultData.FAIL_CODE_MSG);
    }

    @OperRecord(content = "修改设备信息",level = 1)
    @PostMapping("update")
    @ApiOperation(value = "修改设备信息",notes = "修改设备信息")
    public ApiResponse<? extends Object> update(@RequestBody Device device) {
        if (iDeviceService.updateById(device))
            return ok();
        return fail(ResultData.FAIL_CODE_MSG);
    }

    @OperRecord(content = "删除设备",level = 1)
    @PostMapping("del/{id}")
    @ApiOperation(value = "删除设备",notes = "修改设备信息")
    public ApiResponse<? extends Object> del(@PathVariable("id") Long id) {
        if (iDeviceService.removeById(id))
            return ok();
        return fail(ResultData.FAIL_CODE_MSG);
    }

    @HeaderWrapper(DeviceEnum.class)
    @ApiOperation(value = "设备列表 分页|currentPage|size|sortType|sortField 必传", notes = "设备列表 分页|currentPage|size|sortType|sortField 必传")
    @PostMapping("list/{currentPage}/{size}/{sortType}/{sortField}")
    public ApiResponse<IPage<ListDeviceResp>> listMaterial(@RequestBody Map<String ,Object> queryParam,
                                                           @PathVariable("currentPage")Long currentPage,
                                                           @PathVariable("size")Integer size,
                                                           @PathVariable("sortType")Integer sortType,
                                                           @PathVariable("sortField")String sortField) {
        PageParam pageParam = new PageParam(currentPage,size,sortType,sortField);
        return ok(iDeviceService.getListAndPages(queryParam, pageParam));
    }

    @ApiOperation(value = "获取设备类别列表")
    @GetMapping("getDeviceTypes")
    public ApiResponse<List<Device>> getDeviceTypes() {

        return ok(iDeviceService.getDeviceTypes());
    }

    @OperRecord(content = "保养设备",level = 1)
    @ApiOperation(value = "新增保养记录")
    @PostMapping("addMaintenances")
    public ApiResponse<Boolean> addMaintenances(@RequestBody DeviceMaintenance maintenance) {

        if (iDeviceMaintenanceService.save(maintenance)) {
            //修改下次需要保养的时间
            iDeviceService.updateNextMaintenanceDateById(maintenance.getDeviceId());
            return ok();
        }
        return fail("添加失败");
    }


    @ApiOperation(value = "获取设备的保养记录")
    @GetMapping("getMaintenances/{deviceId}")
    public ApiResponse<List<DeviceMaintenance>> getMaintenances(@PathVariable("deviceId")Long deviceId) {
        QueryWrapper<DeviceMaintenance> query = new QueryWrapper<>();
        query.eq("deviceId", deviceId);
        query.orderByDesc("updateTime");
        return ok(iDeviceMaintenanceService.list(query));
    }

    @ApiOperation(value = "获取所有设备以及设备状态")
    @GetMapping("getDeviceListAndStatus")
    public ApiResponse<List<Device>> getDeviceListAndStatus() {
        QueryWrapper<Device> query = new QueryWrapper<>();
        query.orderByAsc("deviceStatus").select("id", "deviceName", "deviceStatus");
        return ok(iDeviceService.list(query));
    }

    @ApiOperation(value = "获取所有待分配设备")
    @GetMapping("getDeviceDFP")
    public ApiResponse<List<Device>> getDeviceDFP() {
        QueryWrapper<Device> query = new QueryWrapper<>();
        query.eq("deviceStatus", DeviceStatusEnum.DFP).select("id", "deviceName");
        return ok(iDeviceService.list(query));
    }

}
